package com.sales.service.data.transaction;

import org.json.JSONException;

import android.util.Log;

import com.sales.service.constants.ServiceConstants;
import com.sales.service.data.transaction.notifier.ResponseListeners;
import com.sales.service.utils.CacheManager;
import com.sforce.android.soap.partner.OAuthLoginResult;

/**
 * This is a abstract class which implements ResponseListeners class.
 * 2 methods are made abstract - initiateTransaction and deserializetion.
 * 3 more methods are declared.
 * 
 * @author Sega
 *
 */
public abstract class AbstractTransaction implements ResponseListeners{

	/**
	 * Strings for debugging process by log.i method
	 */
	private static final String TagProject = "SalesService";
	private static final String TagPage = "AbstractTranc ::";
	
	/**
	 * abstract method to initiate the thread.
	 */
	protected abstract void initiateTransaction();
	
	/**
	 * abstract method to return the result to the activity page
	 * 
	 * @param response - entity response as input
	 * @throws JSONException
	 */
	protected abstract void deserializeJson(String response) throws JSONException;

	/**
	 * method to get the base Url to find the server.
	 * 
	 * @return String is returned as url.
	 */
	public String  getBaseUrl() {
		Object loginOAuthLoginResult = CacheManager.getInstance().getProperty(ServiceConstants.CACHE_OAUTH_KEY);
		OAuthLoginResult result = (OAuthLoginResult) loginOAuthLoginResult;
		String appBaseUrl = result.getInstanceUrl()	+ ServiceConstants.SF_OBJECT_URL;
		//appBaseUrl=encodeRequest(appBaseUrl, "UTF-8");
		return appBaseUrl;

	}

	/**
	 *  
	 * But not used in this app
	 * 
	 * @param url
	 * @param encode
	 * @return
	 */
	protected String encodeRequest(String url, String encode) {
		String encodedRequest = url;
		try {
			encodedRequest = url; //URLEncoder.encode(url, encode);
		} catch (Exception e) {
			Log.i(TagProject, TagPage + "Error in Encoding URL" + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
			e.printStackTrace();
		}
		return encodedRequest;
	}

}